package com.eascs.dubbo.api.internal;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.admin.api.internal.StoreInfoService;
import com.eascs.admin.common.utils.StringUtils;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanBkInfo;
import com.eascs.admin.o2o.loan.entity.O2oLoanBkpInfo;
import com.eascs.admin.o2o.loan.entity.O2oLoanBksyInfo;
import com.eascs.admin.o2o.loan.entity.O2oLoanBkxyInfo;
import com.eascs.admin.o2o.loan.entity.O2oLoanGsInfo;
import com.eascs.admin.o2o.loan.entity.O2oLoanGsMainproductInfo;
import com.eascs.admin.o2o.loan.entity.O2oLoanScInfo;
import com.eascs.admin.o2o.loan.entity.O2oLoanScToptencInfo;
import com.eascs.admin.o2o.loan.entity.O2oLoanScToptensInfo;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanBkpService;
import com.eascs.admin.o2o.loan.service.O2oLoanGsMainProductService;
import com.eascs.admin.o2o.loan.service.O2oLoanScTopTenSService;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.nsEditDetail.dao.MainStoreInfoDao;
import com.eascs.crm.nsEditDetail.entity.ManagementInfoEntity;
import com.eascs.crm.nsEditDetail.entity.O2oMainMonthInfoEntity;
import com.eascs.crm.nsEditDetail.entity.O2oMainPsInfoEntity;
import com.eascs.crm.nsEditDetail.entity.O2oMainShopInfoEntity;
import com.eascs.crm.nsEditDetail.entity.O2oMainStoreInfoEntity;
import com.eascs.crm.nsEditDetail.service.IMainMonthInfoService;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

@Service
public class StoreInfoServiceImpl implements StoreInfoService {

	@Autowired
	MainStoreInfoDao mainStoreInfoDao;

	@Reference
	IMainMonthInfoService mainMonthInfoService;

	@Autowired
	O2oLoanGsMainProductService productService;

	@Autowired
	O2oLoanBkpService o2oLoanBkpService;

	@Autowired
	O2oLoanScTopTenSService o2oLoanScTopTenSService;

	@Override
	public PageResult<Object[]> getEntityByloanapplyNo(String id) {
		String sql = "SELECT * from o2o_main_store_info o WHERE o.loanapply_no=? AND o.is_del='N'";
		SqlPageQuery<Object[]> pageQuery = new SqlPageQuery<>(Object[].class, Integer.MAX_VALUE, 0);
		pageQuery.addParam(id);
		pageQuery.setQuerySql(sql);
		return mainStoreInfoDao.findDataWithPage(pageQuery);
	}

	@Override
	public ManagementInfoEntity getManageByLoanapplyNo(String id) {
		String sql = "SELECT * from management_info t where t.loanapply_no = ?";
		return mainStoreInfoDao.findEntity(sql, ManagementInfoEntity.class, id);
	}

	@Override
	public O2oMainShopInfoEntity getMainShopByLoanapplyNo(String id) {
		String sql = "SELECT * from o2o_main_shop_info t where t.loanapply_no = ?";
		return mainStoreInfoDao.findEntity(sql, O2oMainShopInfoEntity.class, id);
	}

	@Override
	public O2oMainPsInfoEntity getMainPsByLoanapplyNo(String id) {
		String sql = "SELECT * from o2o_main_ps_info t where t.loanapply_no = ?";
		return mainStoreInfoDao.findEntity(sql, O2oMainPsInfoEntity.class, id);
	}

	@Override
	public PageResult<Object[]> getMainproductByloanapplyNoSG(String id) {
		String sql = "SELECT m.id, m.product_name, m.production_address, m.last_month_sale_num, m.loanapply_no, o.loan_type FROM o2o_loan_gs_mainproduct_info m LEFT JOIN o2o_loanapply o ON m.loanapply_no = o.loanapply_no WHERE m.loanapply_no = ?";
		SqlPageQuery<Object[]> pageQuery = new SqlPageQuery<>(Object[].class, Integer.MAX_VALUE, 0);
		pageQuery.addParam(id);
		pageQuery.setQuerySql(sql);
		return mainStoreInfoDao.findDataWithPage(pageQuery);
	}

	@Override
	public PageResult<Object[]> getMainproductByloanapplyNoSC(String id) {
		String sql = "SELECT s.id, s.product_brand, s.product_type, s.agent_quali, s.coop_term, s.y_saleTask, o.loan_type FROM o2o_loan_sc_toptens_info s LEFT JOIN o2o_loanapply o ON s.loanapply_no = o.loanapply_no WHERE s.loanapply_no =?";
		SqlPageQuery<Object[]> pageQuery = new SqlPageQuery<>(Object[].class, Integer.MAX_VALUE, 0);
		pageQuery.addParam(id);
		pageQuery.setQuerySql(sql);
		return mainStoreInfoDao.findDataWithPage(pageQuery);
	}

	@Override
	public PageResult<Object[]> getMainproductByloanapplyNoKPJ(String id) {
		String sql = "SELECT s.id, s.product_name, s.brand, s.model, s.buying_price, s.selling_price, s.last_month_sales, o.loan_type FROM o2o_loan_bkp_info s LEFT JOIN o2o_loanapply o ON s.loanapply_id = o.loanapply_no WHERE s.loanapply_id = ? and s.is_delete ='N' ";
		SqlPageQuery<Object[]> pageQuery = new SqlPageQuery<>(Object[].class, Integer.MAX_VALUE, 0);
		pageQuery.addParam(id);
		pageQuery.setQuerySql(sql);
		return mainStoreInfoDao.findDataWithPage(pageQuery);
	}

	@Override
	public O2oLoanapplyEntity getO2oLoanay(String id) {
		String sql = "SELECT * from o2o_loanapply t where t.loanapply_no = ?";
		return mainStoreInfoDao.findEntity(sql, O2oLoanapplyEntity.class, id);
	}

	@Override
	public O2oLoanGsInfo getUpSG(String id) {
		String sql = "select * from o2o_loan_gs_info where loanapply_no = ?";
		return mainStoreInfoDao.findEntity(sql, O2oLoanGsInfo.class, id);
	}

	@Override
	public O2oLoanScInfo getUpSC(String id) {
		String sql = "select * from o2o_loan_sc_info where loanapply_no = ?";
		return mainStoreInfoDao.findEntity(sql, O2oLoanScInfo.class, id);
	}

	@Override
	public PageResult<Object[]> getUpBJP(String id) {
		String sql = "select * from o2o_loan_bksy_info o where o.loanapply_id= ? and o.is_delete='N' ";
		SqlPageQuery<Object[]> pageQuery = new SqlPageQuery<>(Object[].class, Integer.MAX_VALUE, 0);
		pageQuery.addParam(id);
		pageQuery.setQuerySql(sql);
		return mainStoreInfoDao.findDataWithPage(pageQuery);
	}

	@Override
	public PageResult<Object[]> getDownSC(String id) {
		String sql = "SELECT id, cus_name, settlement_date, supply_quantity, loanapply_no FROM o2o_loan_sc_toptenc_info WHERE loanapply_no =?";
		SqlPageQuery<Object[]> pageQuery = new SqlPageQuery<>(Object[].class, Integer.MAX_VALUE, 0);
		pageQuery.addParam(id);
		pageQuery.setQuerySql(sql);
		return mainStoreInfoDao.findDataWithPage(pageQuery);
	}

	@Override
	public PageResult<Object[]> getDownBPJ(String id) {
		String sql = "SELECT id, customer_name, sales, settlement, payment, loanapply_id FROM o2o_loan_bkxy_info WHERE loanapply_id=? and is_delete='N' ";
		SqlPageQuery<Object[]> pageQuery = new SqlPageQuery<>(Object[].class, Integer.MAX_VALUE, 0);
		pageQuery.addParam(id);
		pageQuery.setQuerySql(sql);
		return mainStoreInfoDao.findDataWithPage(pageQuery);
	}

	@Override
	public O2oLoanBkInfo getChannelBPJ(String id) {
		String sql = "select * from o2o_loan_bk_info o where o.loanapply_id= ?";
		return mainStoreInfoDao.findEntity(sql, O2oLoanBkInfo.class, id);
	}

	@Override
	public O2oMainStoreInfoEntity getStoreInfoEntity(String id) {
		String sql = "SELECT * from o2o_main_store_info o WHERE o.id=? AND o.is_del='N'";
		return mainStoreInfoDao.findEntity(sql, O2oMainStoreInfoEntity.class, id);
	}

	@Override
	public O2oLoanBksyInfo getUpBJPOne(String id) {
		String sql = "select * from o2o_loan_bksy_info o where o.id= ? and o.is_delete='N'";
		return mainStoreInfoDao.findEntity(sql, O2oLoanBksyInfo.class, id);
	}

	@Override
	public O2oLoanScToptencInfo getDownSCOne(String id) {
		String sql = "select * from o2o_loan_sc_toptenc_info o where o.id= ?";
		return mainStoreInfoDao.findEntity(sql, O2oLoanScToptencInfo.class, id);
	}

	@Override
	public O2oLoanBkxyInfo getDownBPJOne(String id) {
		String sql = "select * from o2o_loan_bkxy_info o where o.id= ?";
		return mainStoreInfoDao.findEntity(sql, O2oLoanBkxyInfo.class, id);
	}

	@Override
	public String updateShopInfo(O2oMainMonthInfoEntity o2oMainMonthInfoEntity) {
		User user = UserHolder.getUser();
		String result = null;
		O2oMainMonthInfoEntity entity = mainMonthInfoService.getByLoanNo(o2oMainMonthInfoEntity.getLoanapplyNo());
		if (entity == null) {
			o2oMainMonthInfoEntity.setCreateDate(new Date());
			o2oMainMonthInfoEntity.setModifyDate(new Date());
			o2oMainMonthInfoEntity.setCreateId(user.getNo());
			o2oMainMonthInfoEntity.setModifyId(user.getNo());
			o2oMainMonthInfoEntity.setCreator(user.getCname());
			o2oMainMonthInfoEntity.setModifier(user.getCname());
			boolean flag = mainMonthInfoService.insert(o2oMainMonthInfoEntity);// 新增
			if (!flag) {
				result = "添加近六个月营业额情况失败";
			}
		} else {
			if (StringUtil.isBlank(o2oMainMonthInfoEntity.getId())) {
				result = "更新近六个月营业额情况失败:记录不存在";
			} else {
				O2oMainMonthInfoEntity info = mainMonthInfoService.getEntityById(o2oMainMonthInfoEntity.getId());
				o2oMainMonthInfoEntity.setModifyDate(new Date());
				o2oMainMonthInfoEntity.setModifyId(user.getNo());
				o2oMainMonthInfoEntity.setModifier(user.getCname());
				o2oMainMonthInfoEntity.setCreateId(info.getCreateId());
				o2oMainMonthInfoEntity.setCreateDate(info.getCreateDate());
				o2oMainMonthInfoEntity.setCreator(info.getCreator());
				boolean flag = mainMonthInfoService.update(o2oMainMonthInfoEntity);// 修改
				if (!flag) {
					result = "修改近六个月营业额情况失败";
				}
			}
		}
		return result;
	}

	@Override
	public String insertAndUpdateMainproductBk(O2oLoanBkpInfo o2oLoanBkpInfo) {
		if (StringUtil.isBlank(o2oLoanBkpInfo.getId())) {
			o2oLoanBkpInfo.setIsDelete(StatusContexts.O2O_NO);
			o2oLoanBkpService.insertEntity(o2oLoanBkpInfo);
		} else {
			O2oLoanBkpInfo bkpInfo = o2oLoanBkpService.getEntityById(o2oLoanBkpInfo.getId());
			bkpInfo.setBrand(o2oLoanBkpInfo.getBrand());
			bkpInfo.setBuyingPrice(o2oLoanBkpInfo.getBuyingPrice());
			bkpInfo.setLastMonthSales(o2oLoanBkpInfo.getLastMonthSales());
			bkpInfo.setLoanapplyId(o2oLoanBkpInfo.getLoanapplyId());
			bkpInfo.setModel(o2oLoanBkpInfo.getModel());
			bkpInfo.setProductName(o2oLoanBkpInfo.getProductName());
			bkpInfo.setSellingPrice(o2oLoanBkpInfo.getSellingPrice());
			o2oLoanBkpService.updateEntity(bkpInfo);
		}
		return null;
	}

	@Override
	public int deleteMainproductBk(String id) {
		O2oLoanBkpInfo loanBkpInfo = o2oLoanBkpService.getEntityById(id);
		loanBkpInfo.setIsDelete(StatusContexts.O2O_YES);
		return o2oLoanBkpService.updateEntity(loanBkpInfo);
	}

	@Override
	public String insertAndUpdateMainproductSg(O2oLoanGsMainproductInfo o2oLoanGsMainproductInfo) {
		if (StringUtil.isBlank(o2oLoanGsMainproductInfo.getId())) {
			productService.insertEntity(o2oLoanGsMainproductInfo);
		} else {
			productService.updateEntity(o2oLoanGsMainproductInfo);
		}
		return null;
	}

	@Override
	public int deleteMainproductSg(String id) {
		return productService.delete(id);
	}

	@Override
	public String insertAndUpdateMainproductSc(O2oLoanScToptensInfo o2oLoanScToptensInfo) {
		if (StringUtil.isBlank(o2oLoanScToptensInfo.getId())) {
			o2oLoanScTopTenSService.insertEntity(o2oLoanScToptensInfo);
		} else {
			O2oLoanScToptensInfo info = o2oLoanScTopTenSService.getEntityById(o2oLoanScToptensInfo.getId());
			info.setProductBrand(o2oLoanScToptensInfo.getProductBrand());
			info.setProductType(o2oLoanScToptensInfo.getProductType());
			info.setAgentQuali(o2oLoanScToptensInfo.getAgentQuali());
			info.setCoopTerm(o2oLoanScToptensInfo.getCoopTerm());
			info.setY_saleTask(o2oLoanScToptensInfo.getY_saleTask());
			o2oLoanScTopTenSService.updateEntity(info);
		}
		return null;
	}

	@Override
	public int deleteMainproductSc(String id) {
		return o2oLoanScTopTenSService.delete(id);
	}

	@Override
	public O2oLoanBkpInfo queryO2oLoanBkInfo(String id) {
		return o2oLoanBkpService.getEntityById(id);
	}

	@Override
	public O2oLoanGsMainproductInfo queryO2oLoanSgInfo(String id) {
		return productService.getEntityById(id);
	}

	@Override
	public O2oLoanScToptensInfo queryO2oLoanScInfo(String id) {
		return o2oLoanScTopTenSService.getEntityById(id);
	}

	@Override
	public String getLicenceNameByLoanapplyNo(String id) {
		// TODO Auto-generated method stub
		String sql="SELECT o.licence_name from o2o_main_shop_info o where o.loanapply_no=? ";
		String name=mainStoreInfoDao.findString(sql, id);
		if (StringUtils.isEmpty(name)) {
			sql="SELECT i.licence_name from customer_shop_info i where i.loanapply_no=? and i.shoptype='MAN' LIMIT 1";
			name=mainStoreInfoDao.findString(sql, id);
		}
		return name;
	}
}
